#pragma once

namespace Tmpl {

class Entity;
class EntityBuilding
{
public:
	enum BuildingCondition
	{
		BUILDING_CONDITION_ALIVE		= 1 << 0,
		BUILDING_CONDITION_DEAD			= 1 << 1,
		BUILDING_CONDITION_FINISHED		= 1 << 2,
	};

	void Kill();

	Entity* GetOwner(){return mOwner;}
	Vec2f GetPosTopLeft() const { return mPos; }
	Vec2f GetPos() const { return mPos + Vec2f(mCollisionSizeX*0.5f, mCollisionSizeY*0.5f); }

	// HP
	float GetHP() const {return mHP;}
	float GetHPMax() const {return mHPMax;}
	float GetHPPercent() const { return Max(0.0f,Min(100.0f,mHP * 100.0f / mHPMax));}
	void AddHP(float hp, int entitySource);

	// Collision
	bool* GetCollision() const { return mCollision; }
	char GetCollisionSizeX() const { return mCollisionSizeX;}
	char GetCollisionSizeY() const { return mCollisionSizeY;}
	Vec2f GetCollisionSize(){return Vec2f(float(mCollisionSizeX), float(mCollisionSizeY));}
	void SetCollisionSize(char x, char y);

	// Condition
	void ConditionSetFlags(int flag){mConditionFlags = flag; }
	void ConditionAddFlag(int flag){mConditionFlags |= flag; }
	void ConditionRemoveFlag(int flag){mConditionFlags &= ~flag; }
	bool ConditionHasFlag(int flag) const {return ((mConditionFlags & flag) == flag);}
	int ConditionGetFlags() const {return mConditionFlags; }

private:
	friend class EntityBuildingSystem;

	Entity* mOwner;
	Vec2f mPos;

	bool* mCollision;
	char mCollisionSizeX;
	char mCollisionSizeY;

	int mConditionFlags;
	float mHP, mHPMax;
};

struct UnitData;
class EntityBuildingSystem
{
public:
	static void Init();
	static void Clear();

	static int NewBuilding(Entity* owner, Vec2f pos, const UnitData& data);
	static void RemoveBuilding(int id);
	static EntityBuilding* GetBuilding(int id);

	static JobHandle Update(JobHandle dependency);

private:
	static void UpdateBuildingChunk(int chunk);
};

} // namespace Tmpl